package com.lxx.modules.sys.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.lxx.annotation.PermissionName;
import com.lxx.common.BaseAction;
import com.lxx.modules.sys.biz.ISysPermissionBiz;
import com.lxx.modules.sys.entity.SysPermission;
import com.lxx.utils.Constant;
import com.lxx.utils.DataTableResult;
import com.lxx.utils.NetworkUtil;
import com.lxx.utils.PageInfo;
import com.lxx.utils.Pagination;
import com.lxx.utils.Result;
import com.lxx.utils.ValidationUtils;
import com.lxx.vo.ZTreeVo;

@Controller
@RequestMapping("/sysPermission")
public class SysPermissionController extends BaseAction{
	private static final Logger LOGGER = LoggerFactory.getLogger(SysPermissionController.class);
	
	@Autowired
	private ISysPermissionBiz sysPermissionBiz;
	
	@RequestMapping("/query")
	@ResponseBody
	@PermissionName("查询")
	public Result query(PageInfo pageInfo, HttpServletRequest request) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String beginStr = request.getParameter("begin");
		String endStr = request.getParameter("end");
		Map<String, Object> params = new HashMap<>();
		try {
			if (StringUtils.isNotBlank(beginStr)) {
				params.put("begin", format.parse(beginStr));
			}
			if (StringUtils.isNotBlank(endStr)) {
				params.put("end", format.parse(endStr));
			}
			params.put("keywords", request.getParameter("keywords"));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		int pageIndex = pageInfo.getStart() / pageInfo.getLength() + 1;
		Pagination<SysPermission> pagination = sysPermissionBiz.query(params, pageIndex, pageInfo.getLength());
		DataTableResult dataTableResult = new DataTableResult(pageInfo.getDraw(), 
				pagination.getTotalRow(), pagination.getTotalRow(), pagination.getDatas());
		return Result.success(dataTableResult);
	}
	
	@RequestMapping("/queryTree")
	@ResponseBody
	@PermissionName("查询树")
	public Result queryTree() {
		
		List<SysPermission> perms = sysPermissionBiz.queryAll();
		
		return Result.success(perms);
	}
	
	@RequestMapping("/queryTree4Add")
	@ResponseBody
	@PermissionName("查询用于新增")
	public Result queryTree4Add() {
		
		List<ZTreeVo> perms = sysPermissionBiz.queryTree4Add();
		
		return Result.success(perms);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	@PermissionName("新增")
	public Result add(SysPermission sysPermission) {
		LOGGER.debug("{}", sysPermission);
		if (sysPermission.getPid() == null) {
			sysPermission.setPid(0);
		}
		sysPermission.setDelFlag(false);
		sysPermission.setCreateTime(new Date());
		ValidationUtils.validateForce(sysPermission);
		sysPermissionBiz.save(sysPermission);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@PermissionName("修改")
	public Result update(SysPermission sysPermission) {
		sysPermissionBiz.update(sysPermission);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@PermissionName("删除")
	public Result delete(String ids) {
		sysPermissionBiz.delete(ids);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	@PermissionName("详情")
	public Result detail(Integer id) {
		SysPermission sysPermission = sysPermissionBiz.detail(id);
		return Result.success(sysPermission);
	}

}
